﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using DevExpress.XtraGrid;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;
    using System.Collections;


    public partial class frmDmQuocGia : DevExpress.XtraEditors.XtraForm
    {
        IList<DmQuocGiaEntity> lstDmQuocGiaEntity = null;
        DmQuocGiaBusiness dmQuocGiaBusiness = new DmQuocGiaBusiness();
        GridCheckMarksSelection gridCheckMarksSelection = null;

        public frmDmQuocGia()
        {
            InitializeComponent();

            gridCheckMarksSelection = new GridCheckMarksSelection(gvDmQuocGia);
        }

        void LoadDataGrid(object sender, EventArgs e)
        {
            lstDmQuocGiaEntity = dmQuocGiaBusiness.GetDmQuocGiaList();
            grdDmQuocGia.DataSource = lstDmQuocGiaEntity;
        }

        DmQuocGiaEntity getDmQuocGiaEntity()
        {
            if (gvDmQuocGia.RowCount > 0)
            {
                return (DmQuocGiaEntity)gvDmQuocGia.GetFocusedRow();
            }

            return null;
        }

        void btnThemMoi_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmQuocGia((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();
        }

        void frmDmQuocGia_Load(object sender, EventArgs e)
        {
            Authenticate.IsAuthenticateForm(this);
            LoadDataGrid(sender, e);
        }

        void gvDmQuocGia_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Handled = false;
            }
        }

        void btnSua_Click(object sender, EventArgs e)
        {
            DmQuocGiaEntity dmQuocGiaEntity = getDmQuocGiaEntity();
            if (dmQuocGiaEntity == null) return;

            var frm = new frmCapNhatDmQuocGia((int)ConstantManagerClinic.StateInsertOrUpdate.Update, dmQuocGiaEntity);
            frm.ButtonClicked += new EventHandler(LoadDataGrid);
            frm.ShowDialog();

        }

        void grdDmQuocGia_DoubleClick(object sender, EventArgs e)
        {
            if (btnSua.Enabled == false) return;
            btnSua_Click(sender, e);
        }

        void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void btnXoa_Click(object sender, EventArgs e)
        {
            var deleteObjects = gridCheckMarksSelection.selection;

            if (deleteObjects.Count <= 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi để xóa!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dữ liệu?", ConstantManagerClinic.sTitleWarming, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                gridCheckMarksSelection.ClearSelection();
                return;
            }

            foreach (DmQuocGiaEntity dm in deleteObjects)
            {
                dmQuocGiaBusiness.DeleteDmQuocGia(dm.MaQuocGia);
            }

            gridCheckMarksSelection.ClearSelection();
            LoadDataGrid(sender, e);
        }

    }
}